<template>
  <div class="dashboard-container">
    <div class="app-container">
      <!-- 靠右的按钮 -->
      <page-tools>
        <template v-slot:after>
          <el-button type="primary" size="small" @click="addPremission(1,'0')">添加权限</el-button>
        </template>
      </page-tools>
      <!-- 表格 -->
      <el-table border :data="list" row-key="id">
        <el-table-column align="center" label="名称" prop="name" />
        <el-table-column align="center" label="标识" prop="code" />
        <el-table-column align="center" label="描述" prop="description" />
        <el-table-column align="center" label="操作">
          <template slot-scope="{row}">
            <el-button v-if="row.type===1" type="text" @click="addPremission(2,row.id)">添加</el-button>
            <el-button type="text" @click="editPremission(row.id)">编辑</el-button>
            <el-button type="text" @click="delPremission(row.id)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <add-form ref="addForm" @update="initPermission" />
    </div>
  </div>
</template>

<script>
import { getPermissionList, delPermission, getPermissionDetail } from '@/api/permisson.js'
import { tranListToTreeData } from '@/utils/'
import addForm from './components/add-form.vue'
export default {
  components: { addForm },
  data() {
    return {
      list: []
    }
  },
  created() {
    this.initPermission()
  },
  methods: {
    async initPermission() { // 获取所有权限列表
      const res = await getPermissionList()
      // console.log(res)
      this.list = tranListToTreeData(res, '0')// 将数据转换成树形渲染页面
    },
    addPremission(type, pid) { // 新增,type表示类型,pid表示父标识
      this.$refs.addForm.formData.type = type
      this.$refs.addForm.formData.pid = pid
      this.$refs.addForm.dialogShow = true
    },
    async  delPremission(id) { // 删除权限
      await this.$confirm('是否确认删除该权限')
      await delPermission(id)
      this.$message.success('操作成功')
      this.initPermission()
    },
    // 编辑权限
    async  editPremission(id) {
      const res = await getPermissionDetail(id)
      console.log(res)
      this.$refs.addForm.formData = res
      this.$refs.addForm.dialogShow = true
    }

  }
}
</script>

<style>

</style>
